package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "room:all:";


    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return save(room) ? 1 : 0;

    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据房间 id 获取房间视图对象
     *
     * @param id 房间 id
     * @return 房间视图对象
     */
    @Override
    public RoomVo getRoomById(Long id) {
        String key = CACHE_KEY_PREFIX + "price:" + id;
        Object o = redisTemplate.opsForValue().get(key);
        if (o != null) {
            return (RoomVo) o;
        }
        RoomVo roomVo = roomMapper.selectRoom(id);
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, roomVo,30, TimeUnit.MINUTES);
        return roomVo;
    }

    /**
     * 获取房间中的智能设备及数据
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {


        List<RoomVo> roomVos = roomMapper.getRoomsWithDeviceByFloorId(floorId);
        for (RoomVo roomVo : roomVos) {
            List<DeviceInfo> deviceVos = roomVo.getDeviceVos();
            if (CollUtil.isNotEmpty(deviceVos)){
                for (DeviceInfo deviceVo : deviceVos) {
                    String iotId = deviceVo.getIotId();
                    String str= (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA,iotId);
                    if (StrUtil.isNotEmpty( str)){
                        List<DeviceData> dataList = JSONUtil.toList(str, DeviceData.class);
                        deviceVo.setDeviceDataVos(dataList);
                    }
                }
            }
            List<BedVo> bedVoList = roomVo.getBedVoList();
            if (CollUtil.isNotEmpty(bedVoList)){
                for (BedVo bedVo : bedVoList) {
                    List<DeviceInfo> deviceVos2 = bedVo.getDeviceVos();
                    for (DeviceInfo deviceInfo : deviceVos2) {
                        String iotId = deviceInfo.getIotId();
                        String str= (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA,iotId);
                        if (StrUtil.isNotEmpty( str)){
                            List<DeviceData> dataList = JSONUtil.toList(str, DeviceData.class);
                            deviceInfo.setDeviceDataVos(dataList);
                        }
                    }
                }
            }
        }
        return roomVos;
    }


}
